<?php

session_start();
include_once 'moduleMap.php';

/**
 * This class will work as an Aspect Orinted Feature so we can control the excution of the code
 * in a way we want ... 
 * Mainly will check if the user has access to module or not + check if the module is exist or not
 */
class AOP {

    /**
     * 
     */
    private $debug;
    /**
     * will carry all avaliable modules to All users
     * @var array[Stirng, ...]
     */
    private $OPENED_MODULES;
    /**
     * Modules required auth
     * @var array[Stirng, ...]
     */
    private $CLOSED_MODULES;
    /**
     * Modules and page
     * @var array[Stirng, ...][String,...]
     */
    private $MODULE_MAP;
    private $ALL_MODULES;
    function __construct($debug = false) {
        global $OPENED_MODULES;
        global $CLOSED_MODULES;
        global $MODULE_MAP;
        global $ALL_MODULES;
        $this->OPENED_MODULES = $OPENED_MODULES;
        $this->CLOSED_MODULES = $CLOSED_MODULES;
        $this->MODULE_MAP = $MODULE_MAP;
        $this->debug = $debug;
        $this->ALL_MODULES = $ALL_MODULES;
    }

    /**
     * Fire Authentication to check if user has premession to access this module or not
     * @param $module
     */
    public function fireAuthentication($module, $page) {

        if (!isset($module) || trim($module) == "") {
            if ($this->debug
                )echo" module not found fire";
            return -1;
        }

//        if ((!isset($this->MODULE_MAP[$module][$page]) || ($this->MODULE_MAP[$module][$page]) != 1)) {
//            if ($this->debug
//                )echo" module not found fire";
//            return -1;
//        }
        if ($this->OPENED_MODULES[$module] == 1) {
            if ($this->debug
                )echo" Open module fire";
            return 1;
        }
        if ($this->debug)
            print_r($_SESSION['CURRENT_USER']);
        if ($this->CLOSED_MODULES[$module] == 1 && isset($_SESSION['CURRENT_USER']) && $_SESSION['CURRENT_USER'] != "") {
            $user = $_SESSION['CURRENT_USER'];
            if ($user->role->id == $this->ALL_MODULES[$module]) {
                if ($this->debug)echo" closed module fire";
                return 1;
            }
        }
        if ($this->debug
            )echo"no one fire !!";
        return 0;
    }

    public function FIRE($module, $page) {
        return $this->fireAuthentication($module, $page);
    }

}

?>